<?php

/**
 * Taxamatch-Webservice PHP v1.0.0
 * @author Michael Giddens
 * @link http://www.silverbiology.com
 */

/**
 * Class normalize_csv
 */

class CSVProcess {

/**
 * holds the input data
 * @access private
 * @var integer|string
 */
	private $data;

/**
 * the delimiter for the csv data
 * @access private
 * @var string
 */
	private $delimiter = ',';

/**
 * the input csv file name
 * @access private
 * @var string
 */
	private $filename_input = "input.csv";

/**
 * the output csv file name
 * @access private
 * @var string
 */
	private $filename_output = "output.csv";

/**
 * the csv file delimiter
 * @var string
 */
	public $file_delimiter = ',';

/**
 * the file handle for the input file
 * @var string
 */
	public $handle_input;

/**
 * the file handle for the output file
 * @var string
 */
	public $handle_output;

/**
 * the new columns to be added to the csv file
 * @var string
 */
	public $new_columns;

/**
 * Function to set the input to the class
 * @param mixed $variable class property name
 * @param mixed $value class property value
 */
	public function set($variable,$value = '') {
		$this->data[$variable] = $value;
	}

/**
 * This function writes the column header to the csv file.
 */
	public function write_header() {

		// Write New Header to Output File
		fputcsv( $this->handle_output, $this->new_columns, $this->delimiter );
	
	}
	
/**
 * This function prints the header and forces the data to be streamed as the response.
 * @param mixed $actual_file actual file name
 * @param mixed $output_file desired output file name
 */
	public function download_file( $actual_file, $output_file = "output.csv" ) {

		header("Content-type: application/octet-stream");
		header("Content-Disposition: attachment; filename=\"$output_file\"");

		$fp = fopen( $actual_file, 'r');
		fpassthru($fp);
		fclose($fp);
		
	}


/**
 * This is the function that process the input csv file and does the work.
 * @param string $output_type : output type
 */
	public function process( $output_type = 'file' ) {

		// Assign File
		$this->handle_input = fopen( $this->data['input_file'], "r");
		$this->handle_output = fopen( $this->data['output_file'], "w");

		// Build Header
		$this->build_new_header();
		// Writes column header in 1st row of output.
		$this->write_header();
		
		/**
		*	This will run through the whole data and build the Higer Taxa with
		* parentID relationship to be used in the final species list.
		*/
// 		print '<pre>';
		$i = 0;
		
		$db = select_source( $this->data['source'] );
		$tm = new Taxamatch($db);
		$search_mode = 'normal';

		while (($row = fgetcsv($this->handle_input, 10000, $this->file_delimiter)) !== FALSE) {
		
			// Call taxamatch and process row
			if( $tm->process( $row[0], $search_mode ) ) {
				$tm->generateResponse($cache);
			}
			$data = $tm->getData();			
//			print_r($tm->debug);

			array_push($row,$data['genus']['exact'][0]['genus']);
			array_push($row,$data['genus']['exact'][0]['temp_authority']);
			array_push($row,$data['genus']['exact'][0]['genus_id']);
			array_push($row,$data['genus']['phonetic'][0]['genus']);
			array_push($row,$data['genus']['phonetic'][0]['temp_authority']);
			array_push($row,$data['genus']['phonetic'][0]['genus_id']);
			array_push($row,$data['species']['exact'][0]['genus_species']);
			array_push($row,$data['species']['phonetic'][0]['genus_species']);

			// Write results to output file
			fputcsv( $this->handle_output, $row, $this->delimiter );
			
			print_r($row);
			print_r($data);			
			$i++;
			if($i>0) exit();

		}

		fclose($this->handle_input);
	}

/**
 * Reads the first line and identify columns to be added to the csv
 */
	public function build_new_header() {
		$header_array = array('Genus', 'ScientificName');
		$row = fgetcsv($this->handle_input, 10000, $this->file_delimiter);
		foreach($row as $column) {
			if(in_array($column,$header_array)) {
				$header_flag = 1;
			}
		}
		if($header_flag) {
			$headers = array('genus - exact', 'temp_authority - exact', 'genus_id - exact', 'genus - phonetic', 'temp_authority - phonetic', 'genus_id - phonetic', 'genus_species - exact', 'genus_species - phonetic');
			foreach($headers as $header) {
				array_push($row, $header);
			}
			$this->new_columns = $row;
		}
	}
	
}
?>